package com.zhehekeji.opsys.api.service.validate;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.UserUpdateResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TUser;
import com.zhehekeji.opsys.api.service.validate.args.UserRegisterArgs;

/**
 * @ClassName UserUpdateValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-1-4   上午11:06:02
 */
@Service
public class UserUpdateValidate extends ValidateService<ValidateRets<TUser>, UserRegisterArgs> {

	private final static Logger logger = LoggerFactory.getLogger(UserUpdateValidate.class);
	
	@Override
	public ValidateRets<TUser> validate(UserRegisterArgs validateArgs)
			throws Exception {
		
		String tmpAccount = validateArgs.getAccount();
		//String tmpMobile = validateArgs.getMobile();
		Long tmpCpCode = validateArgs.getCpCode();
		SuperResp resp = validateArgs.getResp();
		ValidateRets<TUser> validateRets = new ValidateRets<TUser>();
		if (StringUtils.isEmpty(tmpAccount)){
			logger.error("更新失败，账号非法！");
			resp.setResultCode(UserUpdateResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("更新失败，账号非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
//		if (tmpCpCode == null || tmpCpCode <= 0){
//			logger.error("更新失败，cpCode非法！");
//			resp.setResultCode(UserUpdateResultCode.PARAMETER_IS_NOT_LEGAL);
//			resp.setResultDesc("更新失败，cpCode非法！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
//		TCompany tmpTCompany = entityDao.get(TCompany.class, "o.code=?1", new Object[]{tmpCpCode});
//		if (tmpTCompany == null){
//			logger.error("更新失败，企业不存在，cpCode非法！");
//			resp.setResultCode(UserUpdateResultCode.PARAMETER_IS_NOT_LEGAL);
//			resp.setResultDesc("更新失败，企业不存在，cpCode非法！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
//		Integer tmpStatus = tmpTCompany.getStatus();
//		if (tmpStatus == 0){
//			logger.error("更新失败，企业已经停用！");
//			resp.setResultCode(UserUpdateResultCode.COMPANY_HAS_BEEN_STOPPED);
//			resp.setResultDesc("更新失败，企业已经停用！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
		TUser tmpTUser = entityDao.get(TUser.class, "o.account=?1", new Object[]{tmpAccount});
		if (tmpTUser == null){
			logger.error("更新失败，用户不存在！");
			resp.setResultCode(UserUpdateResultCode.USER_NOT_EXISTS);
			resp.setResultDesc("更新失败，用户不存在！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		validateRets.setEntity(tmpTUser);
		return validateRets;
		
	}

}

